package com.ruoyi.report_kf.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_kf.domain.bo.ReportKfTlsybgBaDetailBo;
import com.ruoyi.report_kf.domain.vo.ReportKfTlsybgBaDetailVo;
import com.ruoyi.report_kf.domain.ReportKfTlsybgBaDetail;
import com.ruoyi.report_kf.mapper.ReportKfTlsybgBaDetailMapper;
import com.ruoyi.report_kf.service.IReportKfTlsybgBaDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 填料试验检测报告详细Service业务层处理
 *
 * @author zyb
 * @date 2024-11-19
 */
@RequiredArgsConstructor
@Service
@DSTransactional
public class ReportKfTlsybgBaDetailServiceImpl implements IReportKfTlsybgBaDetailService {

    private final ReportKfTlsybgBaDetailMapper baseMapper;

    /**
     * 查询填料试验检测报告详细
     */
    @Override
    public ReportKfTlsybgBaDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询填料试验检测报告详细列表
     */
    @Override
    public TableDataInfo<ReportKfTlsybgBaDetailVo> queryPageList(ReportKfTlsybgBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportKfTlsybgBaDetail> lqw = buildQueryWrapper(bo);
        Page<ReportKfTlsybgBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询填料试验检测报告详细列表
     */
    @Override
    public List<ReportKfTlsybgBaDetailVo> queryList(ReportKfTlsybgBaDetailBo bo) {
        LambdaQueryWrapper<ReportKfTlsybgBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportKfTlsybgBaDetail> buildQueryWrapper(ReportKfTlsybgBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportKfTlsybgBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, ReportKfTlsybgBaDetail::getExcelId, bo.getExcelId());
        lqw.eq(bo.getBaRowNumber() != null, ReportKfTlsybgBaDetail::getBaRowNumber, bo.getBaRowNumber());
        lqw.eq(bo.getQualId() != null, ReportKfTlsybgBaDetail::getQualId, bo.getQualId());
        lqw.eq(StringUtils.isNotBlank(bo.getSymbolValue()), ReportKfTlsybgBaDetail::getSymbolValue, bo.getSymbolValue());
        lqw.eq(bo.getNum1() != null, ReportKfTlsybgBaDetail::getNum1, bo.getNum1());
        lqw.eq(bo.getNum2() != null, ReportKfTlsybgBaDetail::getNum2, bo.getNum2());
        lqw.eq(StringUtils.isNotBlank(bo.getJiszb()), ReportKfTlsybgBaDetail::getJiszb, bo.getJiszb());
        lqw.eq(bo.getJiancjg() != null, ReportKfTlsybgBaDetail::getJiancjg, bo.getJiancjg());
        lqw.eq(StringUtils.isNotBlank(bo.getJiegpd()), ReportKfTlsybgBaDetail::getJiegpd, bo.getJiegpd());
        lqw.eq(StringUtils.isNotBlank(bo.getXuh()), ReportKfTlsybgBaDetail::getXuh, bo.getXuh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancxm()), ReportKfTlsybgBaDetail::getJiancxm, bo.getJiancxm());
        return lqw;
    }

    /**
     * 新增填料试验检测报告详细
     */
    @Override
    public Boolean insertByBo(ReportKfTlsybgBaDetailBo bo) {
        ReportKfTlsybgBaDetail add = BeanUtil.toBean(bo, ReportKfTlsybgBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改填料试验检测报告详细
     */
    @Override
    public Boolean updateByBo(ReportKfTlsybgBaDetailBo bo) {
        ReportKfTlsybgBaDetail update = BeanUtil.toBean(bo, ReportKfTlsybgBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportKfTlsybgBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除填料试验检测报告详细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
